package cn.newgrand.netcall.ui.questionRecord;

import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.jivesoftware.resource.Res;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.util.SwingWorker;
import org.jivesoftware.spark.util.log.Log;

import cn.newgrand.smackx.attachmenttransfer.FileTransferManager;
import cn.newgrand.smackx.attachmenttransfer.OutgoingFileTransfer;

public class AttachmentPanel extends JPanel{

	private static final long serialVersionUID = 1L;
	private JButton addButton;
	private JButton deleteButton;
	private AttachmentContainer attachmentContainer;
	private String questionRecordId;
	private FileTransferManager fileTransferManager;
	
	public AttachmentPanel(String questionRecordId,FileTransferManager fileTransferManager){
		setLayout(new GridBagLayout());
		this.questionRecordId = questionRecordId;
		this.fileTransferManager = fileTransferManager;
		attachmentContainer = new AttachmentContainer();
		addButton = new JButton("增加");
		addButton.setPreferredSize(new Dimension(60,30));
		addButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				showFilePicker();
			}
		});
		deleteButton = new JButton("删除");
		deleteButton.setPreferredSize(new Dimension(60,30));
		JScrollPane attachScollPane = new JScrollPane(attachmentContainer);
		attachScollPane.setPreferredSize(new Dimension(188,320));
        add(attachScollPane, new GridBagConstraints(0, 0, 3, 6, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        add(addButton, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        //add(deleteButton, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	}
	
	private void showFilePicker() {
        SwingWorker worker = new SwingWorker() {
            public Object construct() {
                try {
                    Thread.sleep(10);
                }
                catch (InterruptedException e1) {
                    Log.error(e1);
                }
                return true;
            }

            public void finished() {
                FileDialog fileChooser = SparkManager.getTransferManager().getFileChooser(SparkManager.getChatManager().getChatContainer().getChatFrame(), Res.getString("title.select.file.to.send"));
                fileChooser.setVisible(true);

                if (fileChooser.getDirectory() == null || fileChooser.getFile() == null) {
                    return;
                }

                File file = new File(fileChooser.getDirectory(), fileChooser.getFile());
                OutgoingFileTransfer outTransfer = fileTransferManager.createOutgoingFileTransfer(questionRecordId);
                AttachmentDisplayPanel displayPane = new AttachmentDisplayPanel(file,attachmentContainer,questionRecordId,outTransfer);
                if(attachmentContainer.getAttachFiles().size() > 0){
                	if(attachmentContainer.getAttachFiles().containsKey(file.getName())){
                		JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "不能上传名字相同的附件", Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                    	return;
                    }
                }
				attachmentContainer.addComponent(displayPane,file);
            }
        };
        worker.start();
    }

}
